//
//  HMHomeViewController.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//cell可重用标识符
private let HMHomeViewCellId = "HMHomeViewCellId"

class HMHomeViewController: HMVisitorViewController {
    
    //创建一个ViewModel对象
    private lazy var statusListViewModel : HMStatusListModel = HMStatusListModel()
    //刷新控件
    let refreshControl = HMRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !islogin {
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            return
        }
        setTableViewInfo()
        loadData()
        
        
    }
    // MARK: - 设置tableView
    func setTableViewInfo(){
        
        //注册Cell
        tableView.registerClass(HMStatusCell.self, forCellReuseIdentifier: HMHomeViewCellId)
        //设置数据源方法
        tableView.dataSource = self
        tableView.delegate = self
        //         tableView.rowHeight = 250
        //自动行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //预估高
        tableView.estimatedRowHeight = 200
        //设置footView
        tableView.tableFooterView = activityView
        
        //添加事件
        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
        //添加到tableView
        tableView.addSubview(refreshControl)
        tableView.separatorStyle = .None
        
    }
    func refreshAction(){
        loadData()
        
    }
    
    
    //菊花
    private lazy var activityView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = AppThemeColor
        return view
    }()
}


 // MARK: - tableVie的数据源方法
extension HMHomeViewController :UITableViewDataSource,UITableViewDelegate{

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.dataArr.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
        UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier(HMHomeViewCellId, forIndexPath: indexPath) as! HMStatusCell
            cell.statusViewModel = statusListViewModel.dataArr[indexPath.row]
            return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        
        //如果是最后cell  而且菊花没有动画
        if indexPath.row + 1 == statusListViewModel.dataArr.count && !activityView.isAnimating(){
            
            printLog(indexPath.row)
            //设置动画
            activityView.startAnimating()
            //请求数据
            loadData()
        }
    }
    
}
// MARK: - 请求数据
extension HMHomeViewController {
    
    func loadData(){
        //请求数据
        statusListViewModel.loadData(activityView.isAnimating()) { (isSuccess) in
            
            self.endRefreshing()
            
            if !isSuccess{
                print("请求数据失败")
                return
            }
            print("请求数据成功")
            //刷新UI
            self.tableView.reloadData()
        }
        
    }
    
    //停止动画
    func endRefreshing(){
        self.activityView.stopAnimating()
        self.refreshControl.endRefreshing()
        
    }
}


